﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{

    public class FLTCCRDData : BaseFunction
    {
        
        public FLTCCRDData()
            : base()
        {

        }
        public FLTCCRDData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetData(DataRow dwABPOS)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "CCRD";
            string LO = dwABPOS["LO"].ToString().Trim();
            string LB = dwABPOS["LB"].ToString().Trim();
            string FROMDATE = dwABPOS["fromdate"].ToString().Trim();
            string ENDDATE = dwABPOS["enddate"].ToString().Trim();         
            
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT A.GABBUCHDAT,A.GABLO,A.GABLB,A.GABPARTIE,A.GABLBEHI, "
                    + " A.GABART,A.GABVAR,B.GSHANDLNR,B.GSGARNBEZ,A.GABFARBE,C.GSCBEZ, "
                    + " DECODE(A.GABBS,'21','REC','20','RET','33','CSU','ADJ') AS TYPE, "
                    + " DECODE(A.GABBS,'11',-A.GABKG*0.001,'10',-A.GABKG*0.001,'43',-A.GABKG*0.001,A.GABKG*0.001) AS KG, "
                    + " A.GABBUCHBEN "
                    + " FROM GABEW A,GSDAT B,GSCOLOR C "
                    + " WHERE A.GABMANDANT = B.GSMANDANT(+) AND A.GABART = B.GSART(+) "
                    + " AND A.GABVAR = B.GSVAR(+) AND A.GABMANDANT = C.GSCMANDANT(+) "
                    + " AND A.GABART = C.GSCART(+) AND A.GABVAR = C.GSCVAR(+) "
                    + " AND A.GABFARBE = C.GSCFARBE(+) "
                    + " AND A.GABBUCHDAT>='" + FROMDATE + "' AND A.GABBUCHDAT <='" + ENDDATE + "' "
                    + " AND A.GABMANDANT = '00' AND A.GABLO = '"+LO+"' AND A.GABLB LIKE '"+LB+"%' "
                    + " AND A.GABBS IN ('21','20','33','42','43','10','11') "
                    + " ORDER BY 1,4,5 "                                      
                    ;
                
                Result = Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {                        
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["TYPE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        if (LB == "CC")
                        {
                            if (value == "REC")
                            {
                                value = "发去做色卡";
                            }
                            if (value == "RET")
                            {
                                value = "退回仓库";
                            }
                            if (value == "CSU")
                            {
                                value = "色卡消耗";
                            }
                            if (value == "ADJ")
                            {
                                value = "手工调整";
                            }
                        }
                        if(LB == "RD")
                        {
                            if(value == "REC")
                            {
                                value = "发去研发";
                            } 
                            if (value == "RET")
                            {
                                value = "退回仓库";
                            }
                            if (value == "CSU")
                            {
                                value = "研发消耗";
                            }
                            if (value == "REC")
                            {
                                value = "手工调整";
                            }
                        }                        
                        dw["TYPE"] = value;

                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["GSCBEZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["GSCBEZ"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["GSHANDLNR"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["GSHANDLNR"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["GSGARNBEZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["GSGARNBEZ"] = value;
                    }
                }                
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }        
        public DataSet BuildFLTCCRDData(DataRow dwUI)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetData(dwUI);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }        
        
    }
}